package cn.net.autocode.platform.table;

import cn.net.autocode.core.bean.ResultT;
import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.platform.table.service.TableServ;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("platformTableInfo")
@RequestMapping("/platform/tableInfo")
public class TableInfo {

    private final Dao dictDao;

    private final TableServ tableServ;


    public TableInfo(@Qualifier("dictDao") Dao dictDao, TableServ tableServ) {
        this.dictDao = dictDao;
        this.tableServ = tableServ;
    }

    @RequestMapping("load")
    public ResultT load(@RequestBody JSONObject param){
        JSONObject json = dictDao.queryJSON("select * from ac_table where id=?",param.getString("id"));
        return ResultT.success(json);
    }

    @RequestMapping("change/{fieldName}")
    public ResultT change(@PathVariable("fieldName") String fieldName,@RequestBody JSONObject param){
        switch (fieldName){
            case "remarks":
                dictDao.executeUpdate("update ac_table set remarks=?,is_sync='N' where id=?",param.getString("remarks"),param.getString("id"));
                break;
            case "orderColumn":
                dictDao.executeUpdate("update ac_table set order_column=?,is_sync='N' where id=?",param.getString("orderColumn"),param.getString("id"));
                break;
            case "selectColumn":
                dictDao.executeUpdate("update ac_table set select_column=?,is_sync='N' where id=?",param.getString("selectColumn"),param.getString("id"));
                break;
            case "categoryCode":
                dictDao.executeUpdate("update ac_table set category_code=?,is_sync='N' where id=?",param.getString("categoryCode"),param.getString("id"));
                break;
            default:
                break;

        }

        return ResultT.success();
    }

    @RequestMapping("syncToServer")
    public ResultT syncToServer(@RequestBody JSONObject param){
        String id=param.getString("id");
        return tableServ.syncToServer(id);
    }
}
